﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Reflection;

namespace MMOEng
{
    [Serializable]
    public class GameConfiguration
    {
        public int ResolutionX { get; set; }
        public int ResolutionY { get; set; }
        public bool IsFullscreen { get; set; }
        public bool MultiSampling { get; set; }
        public bool VSync { get; set; }
        public UInt16 GameVersion { get; set; }

        public static GameConfiguration Defaults()
        {
            GameConfiguration gc = new GameConfiguration();
            gc.IsFullscreen = false;
            gc.ResolutionX = 800;
            gc.ResolutionY = 600;
            gc.VSync = false;
            gc.MultiSampling = true;
            gc.GameVersion = 0;
            return gc;
        }

        public void SetGraphiccsSettings(MMOGame game)
        {
            GraphicsDeviceManager gdm = game.GraphicsDeviceManager;
            gdm.IsFullScreen = IsFullscreen;
            gdm.PreferMultiSampling = MultiSampling;
            gdm.SynchronizeWithVerticalRetrace = VSync;
            gdm.PreferredBackBufferWidth = ResolutionX;
            gdm.PreferredBackBufferHeight = ResolutionY;
        }
        public void Save(string file)
        {
            BinaryFormatter bf = new BinaryFormatter();
            Stream stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            bf.Serialize(stream, this);
            stream.Close();
        }
        public static GameConfiguration Load(string file)
        {
            GameConfiguration gc = new GameConfiguration();

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None);
            gc = formatter.Deserialize(stream) as GameConfiguration;
            stream.Close();
            return gc;
        }
    }
}
